package com.bai.Controller;

import com.bai.Pojo.Admin;
import com.bai.Pojo.Department;
import com.bai.Service.DepartmentService;

import com.bai.Utils.DtoResultUtil;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Depart")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    //查询所有部门
    @GetMapping("/Der_table")
    public PageLimitDto QueryAllAdmin(String departmentname, Integer offset, Integer limit){
        PageLimitDto result =departmentService.queryAllDepart(departmentname,offset,limit);
        return result;
    }
    //查询所有部门
    @GetMapping("/chart")
    public PublicResponseBody queryAllDepartChart(){
        return departmentService.queryAllDepartChart();
    }

    //添加
    @RequiresRoles(value={"admin"},logical= Logical.OR)
    @GetMapping("/add")
    public PublicResponseBody add(Department department){
        PublicResponseBody addDepart= departmentService.addDepart(department);
        if (addDepart!=null){
            if (addDepart==null){
                return DtoResultUtil.resultError();
            }
            return addDepart;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }

    //查看
    @GetMapping("/getDetail")
    @RequiresRoles(value={"admin"},logical= Logical.OR)
    public PublicResponseBody getDetail(Integer id){
        if (id!=null){
            PublicResponseBody getDepart = departmentService.getDepart(id);
            if (getDepart!=null){
                if (getDepart==null){
                    return DtoResultUtil.resultError();
                }
                return getDepart;
            }else {
                return DtoResultUtil.resultParamError();
            }
        }
        return DtoResultUtil.resultParamError();
    }

    //删除
    @RequiresRoles(value={"admin"},logical= Logical.OR)
    @GetMapping("/delete")
    public PublicResponseBody delete(Integer id){
        if (id!=null){
            PublicResponseBody result = departmentService.deleteDepart(id);
            if (result==null){
                return DtoResultUtil.resultError();
            }
            return result;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    //修改
    @RequiresRoles(value={"admin"},logical= Logical.OR)

    @PostMapping("/edit")
    public PublicResponseBody edit(Department department){
        PublicResponseBody editDepart= departmentService.editDepart(department);
        if (editDepart!=null){
            if (editDepart==null){
                return DtoResultUtil.resultError();
            }
            return editDepart;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    //得到部门简介

    @GetMapping("/queryDepartDetail")
    public PublicResponseBody  queryDepartDetailsById(Integer id) {
        PublicResponseBody queryDepartDetailsById = departmentService.queryDepartDetailsById(id);
        if (queryDepartDetailsById!=null){
            if (queryDepartDetailsById==null){
                return DtoResultUtil.resultError();
            }
            return queryDepartDetailsById;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }

}
